# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import  pymysql

class Tv567Pipeline:



    def process_item(self, item, spider):


        if item.__class__.__name__=='Tv567Item':
            #print(item['title'])
            pass
        else:
            print(item['titles'],item['ok_count'],item['ok_moves'])
        return item


#基于mysql保存方式
class mysql_propipeline(object):
    conn=None  #初始化数据库对象
    cursor=None #初始化游标对象

    def open_spider(self,spider):

        self.conn=pymysql.connect(host='localhost',port=3306,user='root',password='root',db='test1',charset='utf8')




    def process_item(self, item, spider):
        # title=item['title']
        # conts=item['conts']
        try:
            self.cursor=self.conn.cursor()
            self.cursor.execute('insert into mscc(titles,counts,hrefs) values ("%s","%s","%s")'%(item['titles'],item['ok_count'],item['ok_moves']))
            self.conn.commit()
        except Exception as e:
            print(e)
            self.conn.rollback()#回滚操作
        return item

    def close_spider(self,spider):
        self.cursor.close()
        self.conn.close()